from conan import ConanFile
from conan.tools.cmake import CMake, CMakeToolchain, cmake_layout
from conan.tools.files import apply_conandata_patches, export_conandata_patches, get, load, replace_in_file, save, copy
from conan.tools.scm import Version
import os

required_conan_version = ">=1.53.0"


class RaknetConan(ConanFile):
    name = "raknet"
    package_type = "library"
    url = "https://github.com/conan-io/conan-center-index"
    homepage = "https://github.com/facebookarchive/RakNet/"
    license = "BSD"


    settings = "os", "arch", "compiler", "build_type"
    options = {
        "shared": [True, False],
        "fPIC": [True, False],
    }
    default_options = {
        "shared": False,
        "fPIC": True,
    }

    @property
    def _is_mingw(self):
        return self.settings.os == "Windows" and self.settings.compiler == "gcc"

    def export_sources(self):
        export_conandata_patches(self)

    def config_options(self):
        if self.settings.os == "Windows":
            del self.options.fPIC

    def configure(self):
        if self.options.shared:
            self.options.rm_safe("fPIC")


    def layout(self):
        cmake_layout(self, src_folder="src")

    def source(self):
        get(self, **self.conan_data["sources"][self.version],
            destination=self.source_folder, strip_root=True)

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["RAKNET_ENABLE_SAMPLES"] = False
        if self.options.shared:
            tc.variables["RAKNET_ENABLE_DLL"] = True
        else:
            tc.variables["RAKNET_ENABLE_STATIC"] = True
        tc.variables["RAKNET_GENERATE_INCLUDE_ONLY_DIR"] = True
        tc.generate()

    def _patch_sources(self):
        # apply_conandata_patches(self)
        replace_in_file(self, os.path.join(self.source_folder, "Source", "ReplicaManager3.cpp"), 
                        "GetConnectionByGUID(participantListIn[index], worldId)==false",
                        "GetConnectionByGUID(participantListIn[index], worldId)"
                        )

    def build(self):
        self._patch_sources()
        cmake = CMake(self)
        cmake.configure()
        cmake.build()


    def package(self):
        print("source_folder")
        print(self.source_folder)

        print("package_folder")
        print(self.package_folder)
        
        print("build_folder")
        print(self.build_folder)
        
        copy(self, "LICENSE", self.source_folder, self.package_folder)
        copy(self, "*.h", os.path.join(self.source_folder, "include", "RakNet"), os.path.join(self.package_folder, "include"))
        if self.options.shared:
            copy(self, "*.so", os.path.join(self.build_folder, "Lib", "DLL"), os.path.join(self.package_folder, "lib"))
            # copy(self, "*.h", os.path.join(self.build_folder, "Lib", "DLL"), os.path.join(self.package_folder, "include"))
        else:
            copy(self, "*.a", os.path.join(self.build_folder, "Lib", "LibStatic"), os.path.join(self.package_folder, "lib"))
        # cmake = CMake(self)
        # cmake.install()

    def package_info(self):
        self.cpp_info.set_property("cmake_find_mode", "both")
        self.cpp_info.set_property("cmake_file_name", "raknet")
        self.cpp_info.set_property("cmake_target_name", "raknet::raknet")
        self.cpp_info.set_property("pkg_config_name", "raknet")
        self.cpp_info.libs = ["raknet"]
        self.cpp_info.names["cmake_find_package"] = "raknet"
        self.cpp_info.names["cmake_find_package_multi"] = "raknet"
